package com.lifo.user.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lambdaworks.crypto.SCryptUtil;
import com.lifo.shoppingcart.service.ShoppingCartManagerService;
import com.lifo.user.bean.model.HfUser;
import com.lifo.user.service.HfUserService;
import com.lifo.utils.EncryptionUtils;
import com.lifo.utils.common.messagehandler.JsonStandardMsgBean;
import com.lifo.utils.wrapper.WrapperJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/6/24.
 */
@Slf4j
@RestController
@RequestMapping("/user/")
public class HfUserController {

    @Autowired
    private HfUserService hfUserService;

    @Autowired
    private ShoppingCartManagerService shoppingCartManagerService;

    /**
     * 跳转登陆
     * @return
     */
    @RequestMapping("/login")
    public String goLogin() {
        return "/login";
    }


    /**
     * 跳转注册
     * @return
     */
    @RequestMapping("/register")
    public String goRegister(){
        return "/reg";
    }


    /**
     * 注册
     *
     * @param hfUser
     * @return
     */
    @RequestMapping("/user/registerUser")
    public @ResponseBody
    String registerUser(HfUser hfUser,HttpSession session) {

        //密码加密
        hfUser.setPassword(EncryptionUtils.encrypStr(hfUser.getPassword()));

        //保存到hfUser中
        Integer result = hfUserService.saveHfUser(hfUser);

        if (result > 0) {
            session.setAttribute("mobile", hfUser.getMobile());
            session.setAttribute("userId", hfUser.getUserId());
            return "true";
        }

        return "false";
    }


    /**
     * 查询手机号是否存在
     *
     * @param mobile 手机号
     * @return
     */
    @RequestMapping("/user/checkMobile")
    public @ResponseBody
    String checkMobile(String mobile) {
        Integer result = hfUserService.searchMobileIsExist(mobile);
        //返回值大于0 代表手机号存在
        if (result > 0) {
            return "false";
        }
        return "true";
    }


    /**
     * 注销登陆
     * @param session
     * @return
     */
    @RequestMapping("/user/loginOut")
    public String loginOut(HttpSession session){
        session.invalidate();
        return "redirect:/index";
    }

    /**
     * 登陆
     *
     * @return
     */
    @RequestMapping(value = "/gologin", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    public @ResponseBody
    String login(@RequestBody  HfUser vo) throws JsonProcessingException {
        log.debug("登陆用户信息==>{}",new WrapperJson().toJson(vo));
        //通过账号密码查询用户是否存在
        HfUser hfUser1 = hfUserService.searchHfUserByMobile(vo.getMobile());

        if (hfUser1 != null) {
            //判断密码是否相等
            if(SCryptUtil.check(vo.getPassword().trim(),hfUser1.getPassword())){

                return JsonStandardMsgBean.getMsgJsonWithData(hfUser1);
            }
        }
           return JsonStandardMsgBean.getMsgJson("40001","账号或密码错误，请重试");
    }

}
